package sfin.swt.composite;


import java.util.List;
import java.util.ResourceBundle;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

import sfin.controller.SimulacaoController;
import sfin.swt.data.SimulacaoData;
import sfin.swt.exception.StatizoException;
import sfin.swt.util.MessageUtil;

public class SimulacaoComposite extends Composite{

	private SimulacaoController simulacaoController;
	private MessageUtil messageUtil;
	private Table table;
	private ResourceBundle bundle;
	private Combo cbAno;
	private Combo cbMes;
	
	public SimulacaoComposite(Composite parent, int style, SimulacaoController simulacaoController, ResourceBundle bundle, MessageUtil messageUtil) {
		super(parent, style);
		this.simulacaoController = simulacaoController;
		this.messageUtil = messageUtil;
		this.bundle = bundle;
		initialize();
		fillData();
	}

	private void initialize(){
		
		setLayout(new GridLayout(2,true));
		
		Label label = new Label(this,SWT.NONE);
		label.setText(bundle.getString("simulacao.title"));

		GridData gridData = new GridData();
		gridData = new GridData();
		gridData.horizontalSpan = 2;
		gridData.grabExcessHorizontalSpace = true;
		gridData.horizontalAlignment = GridData.CENTER;
		label.setLayoutData(gridData);

		label = new Label(this,SWT.NONE);
		label.setText(bundle.getString("simulacao.label.year"));
		gridData = new GridData();
		gridData.horizontalAlignment = GridData.END;
		label.setLayoutData(gridData);
		
		cbAno = new Combo(this,SWT.BORDER | SWT.READ_ONLY);
		cbAno.setItems(simulacaoController.getYears());
		cbAno.setEnabled(false);

		gridData = new GridData();
		gridData.horizontalAlignment = GridData.BEGINNING;
		cbAno.setLayoutData(gridData);

		label = new Label(this,SWT.NONE);
		label.setText(bundle.getString("simulacao.label.month"));
		gridData = new GridData();
		gridData.horizontalAlignment = GridData.END;
		label.setLayoutData(gridData);
		
		cbMes = new Combo(this,SWT.BORDER | SWT.READ_ONLY);
		cbMes.setItems(simulacaoController.getMonthNames());

		gridData = new GridData();
		gridData.horizontalAlignment = GridData.BEGINNING;
		cbMes.setLayoutData(gridData);
		
		table = new Table(this, SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION);
		table.setLinesVisible(true);
		table.setHeaderVisible(true);
		
		String[] months = simulacaoController.getMonthNames();
		
		TableColumn column = new TableColumn(table, SWT.NONE);
		column.setWidth(120);
		for (int i = 0; i < months.length; i++) {
			column = new TableColumn(table, SWT.NONE);
			column.setWidth(80);
			column.setText(months[i]);
		}
		
		gridData = new GridData(GridData.FILL_HORIZONTAL);
		gridData.horizontalSpan = 2;
		
		table.setLayoutData(gridData);
		
		final Text val = new Text(this, SWT.BORDER | SWT.SEARCH);
		val.setMessage(bundle.getString("simulacao.value"));
		
		gridData = new GridData();
		gridData.horizontalAlignment = GridData.END;
		val.setLayoutData(gridData);
		
		final Text qt = new Text(this, SWT.BORDER | SWT.SEARCH);
		qt.setMessage(bundle.getString("simulacao.qt"));
		
		gridData = new GridData();
		gridData.horizontalAlignment = GridData.BEGINNING;
		qt.setLayoutData(gridData);
		
		
		Button button = new Button(this, SWT.PUSH);
		button.setText(bundle.getString("simulacao.button.calcular"));
		button.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				simulacaoController.setValue(val.getText());
				simulacaoController.setParcelas(qt.getText());
				simulacaoController.setAno(cbAno.getSelectionIndex());
				simulacaoController.setMes(cbMes.getSelectionIndex());
				fillData();
			}
		});
		
		
		gridData = new GridData();
		gridData.horizontalAlignment = GridData.CENTER;
		gridData.horizontalSpan = 2;
		button.setLayoutData(gridData);
		
		
		
	}
	

	private void fillData(){

		try {
			
			cbAno.select(simulacaoController.getAno());
			cbMes.select(simulacaoController.getMes());
			
			table.removeAll();
			
			List<SimulacaoData> dataList = simulacaoController.getData();
			
			String[] saldoAnterior = new String[13];
			String[] receitas = new String[13];
			
			String[] despesas = new String[13];
			String[] despesaComSimulacao = new String[13];
			String[] saldo = new String[13];
			String[] incluido = new String[13];
			
			saldoAnterior[0] = bundle.getString("simulacao.field.saldo-anterior");
			receitas[0] = bundle.getString("simulacao.field.receitas");
			despesas[0] = bundle.getString("simulacao.field.despesas");
			despesaComSimulacao[0] = bundle.getString("simulacao.field.despesas-com-simulacao");
			saldo[0] = bundle.getString("simulacao.field.saldo");
			incluido[0] = bundle.getString("simulacao.filed.simulacao");
			
			
			for (int i = 1; i <= dataList.size(); i++) {
				SimulacaoData data = dataList.get(i - 1);
				saldoAnterior[i] = data.saldoAnterior;
				receitas[i] = data.receita;
				despesas[i] = data.despesa;
				saldo[i] = data.saldo;
				incluido[i] = data.valorIncluido;
				despesaComSimulacao[i] = data.despesaComSimulacao;
			}
			
			TableItem tableItem = new TableItem(table, SWT.NONE);
			tableItem.setText(saldoAnterior);
			
			tableItem = new TableItem(table, SWT.NONE);
			tableItem.setText(receitas);
			
			tableItem = new TableItem(table, SWT.NONE);
			tableItem.setText(despesas);
			
			tableItem = new TableItem(table, SWT.NONE);
			tableItem.setText(incluido);
			
			tableItem = new TableItem(table, SWT.NONE);
			tableItem.setText(despesaComSimulacao);
			
			tableItem = new TableItem(table, SWT.NONE);
			tableItem.setText(saldo);
			
			
			TableColumn[] columns = table.getColumns();
			
			for (int i = 0; i < columns.length; i++) {
				columns[i].pack();
			}
			
			
		} catch (StatizoException e) {
			messageUtil.showMessage(e,getShell());
		}
		
	}
	
}